package com.ds;

import javax.xml.bind.*;
import java.io.*;
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
 * Created by IntelliJ IDEA.
 * User: daniel
 * Date: 28/10/2009
 * Time: 15:04:16
 */
public class TVShowsConfig {

    private static Logger logger = Logger.getLogger("rsstorrent");
    public static Date START_DATE;

    private Shows shows;
    private static final int DAYS_BACK_TO_CHECK = 15;


    public TVShowsConfig() {
        Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        cal.add(Calendar.DAY_OF_YEAR,-DAYS_BACK_TO_CHECK);
        START_DATE = cal.getTime();
    }

    void marshal() throws JAXBException, FileNotFoundException {

        Shows shows = new Shows();
        TVShow tvs = new TVShow();
        tvs.setName("Californication");
        tvs.setFilter("eztv");
        tvs.setNot("promo 720p");
        tvs.setRssURL("http://ezrss.it/search/index.php?show_name=Californication&show_name_exact=true&date=&quality=HDTV&release_group=&mode=rss");
        shows.add(tvs);

        FileOutputStream fos = new FileOutputStream("config.xml");


        JAXBContext jc = JAXBContext.newInstance("com.ds");
        //Create marshaller
        Marshaller m = jc.createMarshaller();
        //Marshal object into file.
        m.marshal(shows, fos);

    }

    public void unmarshal() throws JAXBException, FileNotFoundException {


        JAXBContext jc = JAXBContext.newInstance("com.ds");
        //Create unmarshaller
        Unmarshaller um = jc.createUnmarshaller();
        //Unmarshal XML contents of the file myDoc.xml into your Java object instance.
        FileInputStream configFile = new FileInputStream("config.xml");

        shows = (Shows) um.unmarshal(configFile);
        logger.fine("TVShowsConfig: configuration file finished reading ok : ");

    }

    public Shows getShows() {
        return shows;
    }


    public static void main(String[] args) throws JAXBException, FileNotFoundException {

//        new TVShowsConfig().marshal();

        TVShowsConfig config = new TVShowsConfig();
        config.unmarshal();

        Shows shows = config.getShows();
        System.out.println("shows.getMyEpisodesRSSLink() = " + shows.getMyEpisodesRSSLink());
        System.out.println("shows = " + shows);


    }

}
